<?php

/**
 * This is the model class for table "repayments".
 *
 * The followings are the available columns in table 'repayments':
 * @property integer $id
 * @property integer $credit_id
 * @property integer $user_id
 * @property double $sum
 * @property string $created_at
 */
class Repayments extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Repayments the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{repayments}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('credit_id, user_id, sum, created_at', 'required'),
			array('credit_id, user_id', 'numerical', 'integerOnly'=>true),
			array('sum', 'numerical', 'message'=>'Сумма должна быть числом.'),
			array('sum', 'checkSum'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, credit_id, user_id, sum, created_at', 'safe', 'on'=>'search'),
		);
	}

	public function checkSum()
	{
		$credit = Credits::model()->findByPk($this->credit_id);
		if($this->sum > ($credit->sum - $credit->repaymentsSum)){
			$this->addError('sum','Сумма взноса превышает сумму задолженности.');
		}
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'credit'=>array(self::BELONGS_TO,'Credits','credit_id')
		);
	}

	public function behaviors(){
		return array(
			'createUpdateBehavior' => array(
				'class' => 'application.behaviors.CreateDateBehavior',
				'createdField' => 'created_at'
			),
			'createRepaymentBehavior' => array(
				'class' => 'application.behaviors.CreateRepaymentBehavior'
			)
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'credit_id' => 'Договор',
			'user_id' => 'Оператор',
			'sum' => 'Сумма',
			'created_at' => 'Дата платежа',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('credit_id',$this->credit_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('sum',$this->sum);
		$criteria->compare('created_at',$this->created_at,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}